﻿namespace Chain.BLL
{
    using Chain.IDAL;
    using Chain.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;

    public class GoodsClass
    {
        private readonly Chain.IDAL.GoodsClass dal = new Chain.IDAL.GoodsClass();

        public int Add(Chain.Model.GoodsClass model)
        {
            return this.dal.Add(model);
        }

        public void CreateTreeItem(DataTable dtSource, DataTable dt, int ParentClassID, int level)
        {
            if (dt.Columns.Count <= 0)
            {
                for (int j = 0; j < dtSource.Columns.Count; j++)
                {
                    dt.Columns.Add(new DataColumn(dtSource.Columns[j].ColumnName));
                }
            }
            DataRow[] rowArray = dtSource.Select(" ParentID=" + ParentClassID);
            for (int i = 0; i < rowArray.Length; i++)
            {
                dt.Rows.Add(dt.NewRow());
                for (int k = 0; k < dt.Columns.Count; k++)
                {
                    string str = rowArray[i][k].ToString();
                    if (dt.Columns[k].ColumnName == "ClassName")
                    {
                        str = new string('-', level * 3).ToString() + str;
                    }
                    dt.Rows[dt.Rows.Count - 1][k] = str;
                }
                int parentClassID = Convert.ToInt32(rowArray[i]["ClassID"].ToString());
                this.CreateTreeItem(dtSource, dt, parentClassID, level + 1);
            }
        }

        public List<Chain.Model.GoodsClass> DataTableToList(DataTable dt)
        {
            List<Chain.Model.GoodsClass> list = new List<Chain.Model.GoodsClass>();
            int count = dt.Rows.Count;
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    Chain.Model.GoodsClass item = new Chain.Model.GoodsClass();
                    if ((dt.Rows[i]["ClassID"] != null) && (dt.Rows[i]["ClassID"].ToString() != ""))
                    {
                        item.ClassID = int.Parse(dt.Rows[i]["ClassID"].ToString());
                    }
                    if ((dt.Rows[i]["ClassName"] != null) && (dt.Rows[i]["ClassName"].ToString() != ""))
                    {
                        item.ClassName = dt.Rows[i]["ClassName"].ToString();
                    }
                    if ((dt.Rows[i]["ClassRemark"] != null) && (dt.Rows[i]["ClassRemark"].ToString() != ""))
                    {
                        item.ClassRemark = dt.Rows[i]["ClassRemark"].ToString();
                    }
                    if ((dt.Rows[i]["ParentID"] != null) && (dt.Rows[i]["ParentID"].ToString() != ""))
                    {
                        item.ParentID = int.Parse(dt.Rows[i]["ParentID"].ToString());
                    }
                    if ((dt.Rows[i]["CreateShopID"] != null) && (dt.Rows[i]["CreateShopID"].ToString() != ""))
                    {
                        item.CreateShopID = int.Parse(dt.Rows[i]["CreateShopID"].ToString());
                    }
                    list.Add(item);
                }
            }
            return list;
        }

        public bool Delete(int ClassID)
        {
            return this.dal.Delete(ClassID);
        }

        public int DeleteByParentID(int ParentID)
        {
            return this.dal.DeleteByParentID(ParentID);
        }

        public int DeleteClassByShop(int ClassID, int ShopID)
        {
            Chain.BLL.GoodsNumber number = new Chain.BLL.GoodsNumber();
            Chain.BLL.GoodsClassAuthority authority = new Chain.BLL.GoodsClassAuthority();
            Chain.BLL.GoodsClassDiscount discount = new Chain.BLL.GoodsClassDiscount();
            Chain.BLL.GoodsClass class2 = new Chain.BLL.GoodsClass();
            new Chain.BLL.Goods();
            Chain.Model.GoodsClass model = class2.GetModel(ClassID);
            if (model == null)
            {
                return -100;
            }
            if ((model.ParentID == 0) && (authority.GetClassCountByParentID(model.ClassID) > 0))
            {
                if (authority.GetClassCountByParentID(model.ClassID, ShopID) > 0)
                {
                    return -2;
                }
                if (number.GetGoodsCount(ClassID, ShopID) == 0)
                {
                    authority.DeleteAuthority(ClassID, ShopID);
                    return 1;
                }
                return -1;
            }
            if (number.GetGoodsCount(ClassID, ShopID) != 0)
            {
                return -1;
            }
            authority.DeleteAuthority(ClassID, ShopID);
            discount.DeleteDiscount(ClassID, ShopID);
            DataTable table = authority.GetShopIDListByClass(ClassID).Tables[0];
            bool flag = false;
            foreach (DataRow row in table.Rows)
            {
                if (Convert.ToInt32(row["ShopID"]) != ShopID)
                {
                    flag = true;
                    break;
                }
            }
            if (!flag)
            {
                class2.Delete(ClassID);
            }
            return 1;
        }

        public bool DeleteList(string ClassIDlist)
        {
            return this.dal.DeleteList(ClassIDlist);
        }

        public bool Exists(int ClassID)
        {
            return this.dal.Exists(ClassID);
        }

        public bool Exists(string ClassName, int ShopID)
        {
            return this.dal.Exists(ClassName, ShopID);
        }

        public bool Exists(int classID, string ClassName, int CreateShopID)
        {
            return this.dal.Exists(classID, ClassName, CreateShopID);
        }

        public DataSet GetAllList()
        {
            return this.GetList("");
        }

        public DataTable GetItem(int ClassID)
        {
            return this.dal.GetItem(ClassID).Tables[0];
        }

        public DataSet GetList(string strWhere)
        {
            return this.dal.GetList(strWhere);
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            return this.dal.GetList(Top, strWhere, filedOrder);
        }

        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            return this.dal.GetListByPage(strWhere, orderby, startIndex, endIndex);
        }

        public DataSet GetListByShopID(int ShopID)
        {
            return this.dal.GetListByShopID(ShopID);
        }

        public int GetMaxId()
        {
            return this.dal.GetMaxId();
        }

        public Chain.Model.GoodsClass GetModel(int ClassID)
        {
            return this.dal.GetModel(ClassID);
        }

        public List<Chain.Model.GoodsClass> GetModelList(string strWhere)
        {
            DataSet list = this.dal.GetList(strWhere);
            return this.DataTableToList(list.Tables[0]);
        }

        public int GetRecordCount(string strWhere)
        {
            return this.dal.GetRecordCount(strWhere);
        }

        public bool Update(Chain.Model.GoodsClass model)
        {
            return this.dal.Update(model);
        }

        public int UpdateByShop(Chain.Model.GoodsClass model)
        {
            if (this.Exists(model.ClassID, model.ClassName, model.CreateShopID))
            {
                return -1;
            }
            if (!this.dal.Update(model))
            {
                return -3;
            }
            return 1;
        }
    }
}

